package gui;

import hcs.HouseController;
import hcs.interfaces.StateObserver;
import hcs.model.Boiler;
import hcs.model.Environment;
import hcs.model.Room;
import hcs.model.RoomSettings;

import java.awt.Dimension;
import java.util.Observable;

import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * 
 * @author vincenzo.delia@polito.it
 *
 */

@SuppressWarnings("serial")
public class LogViewer extends JDialog implements StateObserver {
	
	private JTextArea textArea;
	private HcsGuiMenu fileMenu;

	public LogViewer(HcsGuiMenu f) {
		super();
		this.fileMenu = f;
		textArea = new JTextArea();
		JScrollPane scrollPane = new JScrollPane(textArea,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
		        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		textArea.setEditable(false);
		this.getContentPane().add(scrollPane);
		this.setSize(new Dimension(300,200));
	}

	/**
	 * When hidden, it stops to observe rooms and environment
	 */
	public void setVisible(boolean show) {
		HouseController hc = fileMenu.getCommandMenuBar().getMainFrame().getHouseController();
		if(show) {
			for(Room r : hc) {
				r.addObserver(this);
				r.getSettings().addObserver(this);
			}
			hc.addBoilerObserver(this);
			hc.getEnvironment().addObserver(this);
		} else {
			for(Room r : hc) {
				r.deleteObserver(this);
				r.getSettings().addObserver(this);
			}
			hc.deleteBoilerObserver(this);
			hc.getEnvironment().deleteObserver(this);
			this.textArea.setText(null);
		}
		super.setVisible(show);
		
	}
	
	public void update(Observable o, Object arg) {
		HouseController hc = fileMenu.getCommandMenuBar().getMainFrame().getHouseController();
		if(this.textArea.getLineCount() > 300) {
			this.textArea.setText(null);
		}
		long secs = hc.getEnvironment().getSecondsSinceStart();
		this.textArea.append("[" + secs + "] ");
		String toPrint = null;
		if (o instanceof Environment) {
			Environment e = (Environment) o;
			toPrint = serializeEnvironment(e);			
		}
		if (o instanceof Room) {
			Room r = (Room) o;
			toPrint = serializeRoom(r);
		}
		if(o instanceof RoomSettings) {
			RoomSettings rs = (RoomSettings)o;
			toPrint = serializeSettings(rs);
		}
		if(o instanceof Boiler) {
			Boiler b = (Boiler)o;
			toPrint = serializeBoiler(b);
		}
		this.textArea.append(toPrint);
	}

	private String serializeBoiler(Boiler b) {
		String ret = "Boiler:\n" +
				"\ttemperature: " + b.getCurrentTemperature() + "\n" +
				"\ttarget temperature: " + b.getTargetTemperature() + "\n";
		return ret;
	}

	private String serializeSettings(RoomSettings rs) {
		String ret = rs.getRoom().getName() + "\n" +
				"\tStandardPresenceTemp: " + String.format("%.1f",rs.getStandardPresenceTemp()) + "\n" +
				"\tStandardAwayTemp: " + String.format("%.1f", rs.getStandardAwayTemp()) + "\n" +
				"\tStandardHeatingTime: " + rs.getStandardHeatingTime() + "\n" +
				"\tStandardAwayTime: " + rs.getStandardAwayTime() + "\n" +
				"\tStandardInsideTime: " + rs.getStandardInsideTime() + "\n";
		return ret;
	}

	private String serializeRoom(Room r) {
		String ret = r.getName() + "\n" +
		"\tsqm: " + r.getSqm() + "\n" +
		"\ttemperature: " + String.format("%.1f", r.getTemperature()) + "\n" +
		"\twindow state: " + r.getWindowStatus() + "\n" +
		"\tpresence: " + r.SomebodyHere() + "\n" +
		"\theater: " + String.format("%.1f", r.getHeaterCurrent()) + "\n" +
		"\theater target: " + String.format("%.1f", r.getHeaterTarget()) + "\n";
		return ret;
	}

	private String serializeEnvironment(Environment e) {
		String ret = "Environment\n" +
		"\ttemperature: " + String.format("%.1f", e.getTemperature()) + "\n" +
		"\tit's raining: " + e.isRaining() + "\n"; 
		return ret;
	}	
	
}
